package com.baomidou.ant.weChat.controller;

import com.baomidou.ant.common.Result;
import com.baomidou.ant.service.IReportOrderService;
import com.baomidou.ant.utils.MyStringUtil;
import com.baomidou.ant.vo.response.ResponseTodayDataVo;
import com.baomidou.ant.weChat.config.WxConfig;
import com.baomidou.ant.weChat.model.AccessToken;
import com.baomidou.ant.weChat.model.MessageModel;
import com.baomidou.ant.weChat.service.SendMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 企业微信方法控制层
 */
@RestController
@RequestMapping("/we-chat")
public class WeChatController {
    //企业微信电感部门id--@all表示忽略部门限定
    private final static String dgDeptId = "@all";
    //企业微信汽电部门id--@all表示忽略部门限定
    private final static String qdDeptId = "@all";
    //企业微信通信部门id--@all表示忽略部门限定
    private final static String txDeptId = "@all";
    //电感部门消息推送对象(多个目标请以"|"分割)
    private final static String dgDeptUser = "F16531|LiuZhiQiang|F17847";
    //汽电部门消息推送对象(多个目标请以"|"分割)
    private final static String qdDeptUser  = "";
    //通信部门消息推送对象(多个目标请以"|"分割)
    private final static String txDeptUser  = "";
    @Autowired
    private IReportOrderService reportOrderService;

    @PostMapping("/getToken")
    public Result getToken(){
        AccessToken accessToken= WxConfig.getAccessTokenTwo();
        return Result.getSuccess().setData(accessToken.getAccess_token());
    }

    /**
     * 发送消息给电感生产
     * @return
     */
    @PostMapping("/sendMessageToDG")
    public Result sendMessageToDG(){
        MessageModel messageModel=new MessageModel();
        //发送给指定部门和用户
        messageModel.setToUser(dgDeptUser);
        messageModel.setToParty(dgDeptId);
        //内容设定
        ResponseTodayDataVo vo=reportOrderService.getTodayData("电感事业部");
        String content="电感事业部  生产数据推送,请查收\n";
        content+="●今日各站流出统计：\n";
        for(int i=0;i<vo.getProcess().length;i++){
            content+=vo.getProcess()[i]+":"+vo.getPutAmount()[i]+"\n";
        }
        content+="----------------------------------------------------\n";
        content+="●剩余在制工单: \n";
        for(int i=0;i<vo.getOrders().length;i++){
            content+=(i+1)+","+vo.getOrders()[i]+":"+"已产数量"+vo.getAmount()[i]+";"+"不良数"+vo.getBadAmount()[i]+";"+"今日报工数量："+vo.getReportNums()[i]+"\n";
        }
        content+="----------------------------------------------------\n";
        content+="●在制工单各站滞留统计: \n";
        for(int i=0;i<vo.getOrders().length;i++){
            content+=(i+1)+","+vo.getOrders()[i]+":\n";
            for(int j=0;j<vo.getAllOrderProcess().get(i).length;j++){
                content+="--"+vo.getAllOrderProcess().get(i)[j]+":"+vo.getLots().get(i)[j]+"单\n";
            }
        }
        //System.out.println(content);
        try {
            //执行发送方法
            //先判断是否超过1600个字节
            if(MyStringUtil.getStringLength(content)>1600){
                //统计有几个1600
                int n=MyStringUtil.getLength(content,1600);
                //按统计个数和1600来分割
                String[] contents=MyStringUtil.divide(content,n,1600);
                //分批次上报
                for(int i=0;i<contents.length;i++){
                    if(i!=0){//如果不是第一次推送
                        messageModel.setContent("(承接上文.....)\n"+contents[i]);
                        SendMessageService.sendWeChatMessage(messageModel);
                    }else {
                        messageModel.setContent(contents[i]);
                        SendMessageService.sendWeChatMessage(messageModel);
                    }
                    Thread.sleep(3000);//延迟3s
                }
            }else{
                //直接内容发送
                messageModel.setContent(content);
                SendMessageService.sendWeChatMessage(messageModel);
            }
            return Result.getSuccess().setMsg("发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.getFailure().setMsg("失败");
        }
    }

    /**
     * 发送消息给汽电生产
     * @return
     */
    @PostMapping("/sendMessageToQD")
    public Result sendMessageToQD(){
        MessageModel messageModel=new MessageModel();
        //发送给指定部门和用户
        messageModel.setToUser(qdDeptUser);
        messageModel.setToParty(qdDeptId);
        //内容设定
        messageModel.setContent("报工系统消息推送,请查收\n" +
                "●今日流出统计：\n" +
                "绕线站：1\n" +
                "点胶站：1\n" +
                "外检站：1\n" +
                "测包站：1\n" +
                "终检：1\n" +
                "总计：5\n");
        try {
            //执行发送方法
            SendMessageService.sendWeChatMessage(messageModel);
            return Result.getSuccess().setMsg("发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.getFailure().setMsg("失败");
        }
    }

    /**
     * 发送消息给通信生产
     * @return
     */
    @PostMapping("/sendMessageToTX")
    public Result sendMessageTX(){
        MessageModel messageModel=new MessageModel();
        //发送给指定部门和用户
        messageModel.setToUser(txDeptUser);
        messageModel.setToParty(txDeptId);
        //内容设定
        messageModel.setContent("报工系统消息推送,请查收\n" +
                "●今日流出统计：\n" +
                "绕线站：1\n" +
                "点胶站：1\n" +
                "外检站：1\n" +
                "测包站：1\n" +
                "终检：1\n" +
                "总计：5\n");
        try {
            //执行发送方法
            SendMessageService.sendWeChatMessage(messageModel);
            return Result.getSuccess().setMsg("发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.getFailure().setMsg("失败");
        }
    }


}
